package com.neuedu.his.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.his.po.Expenseclass;            // ← 你的实体类，字段全小写
import com.neuedu.his.service.ExpenseclassService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 非药品收费项目管理
 */
@RestController
@RequestMapping("/api/master/service")
public class ServiceController {

    @Autowired
    private ExpenseclassService serviceService;

    // ========================= 分页查询 =========================
    @GetMapping
    public IPage<Expenseclass> page(
            @RequestParam(defaultValue = "1") long page,
            @RequestParam(defaultValue = "10") long pageSize,
            @RequestParam(required = false) String keyword) {

        LambdaQueryWrapper<Expenseclass> qw = new LambdaQueryWrapper<>();
        qw.eq(Expenseclass::getDelmark, 1);

        if (StringUtils.isNotBlank(keyword)) {
            qw.and(w -> w.like(Expenseclass::getExpcode, keyword)
                    .or().like(Expenseclass::getExpname, keyword));
        }
        qw.orderByAsc(Expenseclass::getId);   // 如有顺序号可改成顺序号

        return serviceService.page(new Page<>(page, pageSize), qw);
    }

    // ========================= 新增 / 修改 =========================
    @PostMapping
    public boolean create(@RequestBody Expenseclass dto) {
        dto.setDelmark(1);
        return serviceService.save(dto);
    }

    @PutMapping("/{id}")
    public boolean update(@PathVariable Integer id, @RequestBody Expenseclass dto) {
        dto.setId(id);
        return serviceService.updateById(dto);
    }

    // ========================= 逻辑删除 =========================
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable Integer id) {
        Expenseclass tmp = new Expenseclass();
        tmp.setId(id);
        tmp.setDelmark(0);
        return serviceService.updateById(tmp);
    }
}
